package com.coolbi.onsupai.core.biz.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.CouponBiz;
import com.coolbi.onsupai.core.dao.CouponDao;
import com.coolbi.onsupai.core.entity.Coupon;
import com.coolbi.onsupai.core.exception.ServiceException;
import com.coolbi.onsupai.core.util.CouponUtils;

public class CouponBizImpl implements CouponBiz{
	@Autowired
	private CouponDao couponDao;
	
	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}

	@Override
	public Coupon save(Coupon obj) {
		try {
			couponDao.save(obj);
		} catch (Exception e) {
			throw new ServiceException("优惠券信息插入错误！",e);
		}
		return obj;
	}

	@Override
	public void delete(long id) {
		try {
			couponDao.delete(id);
		} catch (Exception e) {
			throw new ServiceException("优惠券信息删除错误！",e);
		}
	}

	@Override
	public void update(Coupon obj) {
		try {
			couponDao.update(obj);
		} catch (Exception e) {
			throw new ServiceException("优惠券信息更新错误！",e);
		}
		
	}

	@Override
	public Coupon get(long id) {
		return couponDao.get(id);
	}

	@Override
	public List<Coupon> list() {
		return couponDao.list();
	}

	@Override
	public List<Coupon> listByPage(int offset, int limit) {
		return couponDao.listByPage(offset, limit);
	}

	@Override
	public List<Coupon> find(Map<String, Object> parameter) {
		return couponDao.find(parameter);
	}

	@Override
	public List<Coupon> findByPage(Map<String, Object> parameter, int offset,
			int limit) {
		return couponDao.findByPage(parameter, offset, limit);
	}

	@Override
	public List<Coupon> search(Map<String, Object> parameter, int offset,
			int limit) {
		return couponDao.search(parameter, offset, limit);
	}

	@Override
	public int count() {
		return couponDao.count();
	}

	@Override
	public int countByKeyWord(Map<String, Object> parameter) {
		return couponDao.countByKeyWord(parameter);
	}

	@Override
	public int count(Map<String, Object> parameter) {
		return couponDao.count(parameter);
	}

	@Override
	public Coupon findByCouponNo(String couponNo, Long customerId, Integer status) {
		Coupon coupon = couponDao.findByCouponNo(couponNo, customerId, status);
		CouponUtils.validateCoupon(coupon);
		return coupon;
	}

	@Override
	public void updateCouponStatus(String couponNo, Long customerId, Integer status) {
		if (couponDao.updateCouponStatus(couponNo, customerId, status) < 0) {
			throw new ServiceException("修改优惠券使用状态失败");
		}
	}

}
